<?php 
    /**
    * 
    */
    class CircleAction extends CommonAction
    {
        public function index()
        {
            $Data = M('Circle'); // 实例化Data数据对象
            import('ORG.Util.Page');// 导入分页类
            $count      = $Data->count();// 查询满足要求的总记录数
            $Page       = new Page($count, 8);// 实例化分页类 传入总记录数
            $nowPage = isset($_GET['p'])?$_GET['p']:1;
            $this->page = $Page->data();

            $this->circle = $Data->join("dict_document dd on dd.d_id = dict_circle.c_id")
                                 ->join("dict_user du on du.u_id = dict_circle.master")
                                 ->field("dd.docname, dict_circle.intro, dict_circle.createtime,du.username, dict_circle.c_id")
                                 ->where('dict_circle.status != 0')
                                 ->page($nowPage, 8)->select();

            $this->hotCircle = $Data->join("dict_document dd on dd.d_id = dict_circle.c_id")
                                 ->join("dict_user du on du.u_id = dict_circle.master")
                                 ->field("dd.docname, dict_circle.intro, dict_circle.createtime,du.username, dict_circle.c_id, dict_circle.hot")
                                 ->where('dict_circle.status != 0')
                                 ->order('hot desc')
                                 ->page(0, 5)->select();                     
            // $this->hotCircle = $Data->limit(0, 5)->order("hot desc")->select();

            $this->display("index");
            // p($this->page, "page");
            // p($this->circle, "circle");
            // p($this->hotCircle, "hotCircle");
            // p($count);
        }

        public function circle()
        {
            $c_id = I("c_id");
            if (!empty($c_id)) {
                $this->circle_user = D("CircleUser")->selectAllUserByCid($c_id);
                $this->circle = M("Circle")->where("c_id = $c_id")
                                           ->join("dict_document dd on dd.d_id = dict_circle.c_id")
                                           ->field("dd.docname, dict_circle.c_id, dict_circle.intro, dict_circle.createtime, dict_circle.hot")
                                           ->find();

                $thesis = D("Thesis")->where("d_id = $c_id")
                                           ->join("dict_user du on du.u_id = dict_thesis.u_id")
                                           ->field("du.username, dict_thesis.*")
                                           ->select();

                for ($i=0; $i < count($thesis); $i++) { 
                    $thesis[$i]['content'] = readFormFile($thesis[$i]['thesis']);
                }
                $this->thesis = $thesis;

                $this->user = D("CircleUser")->selectAllUserByCid($c_id);
                $this->u_id = session(C("SESSION_USER_ID"));
                $this->display("circle_detail");
            } else {
                halt("页面不存在！");
            }

            p($this->circle_user, "circle_user");
            // p($this->circle, "circle");
            // p($this->thesis, "thesis");
            // p($this->user, "user");
            p($this->u_id, "u_id");
        }

        public function thesis()
        {
            $t_id = I("t_id");
            if (empty($t_id)) {
                halt("页面不存在！");
            } else {
                $thesis = M("Thesis")->where("t_id = $t_id")
                                           ->join("dict_user du on du.u_id = dict_thesis.u_id")
                                           ->join("dict_document dd on dd.d_id = dict_thesis.d_id")
                                           ->field("du.username, dict_thesis.*, dd.docname")
                                           ->find();

                // for ($i=0; $i < count($thesis); $i++) { 
                $thesis['content'] = readFormFile($thesis['thesis']);
                // }
                $this->thesis = $thesis;

            }
            
            $this->display("view_detail");

            p($this->thesis, "thesis");
        }

        public function updateThesisHandle()
        {
            if(!IS_POST) halt("页面不存在！");
            $t_id = I("t_id");
            $content = I("editorValue");
            $model = new Model();
            $path = "./Public/Home/file/thesis" . $t_id . ".txt";
            writeToFile($path, $content);

            $data['t_id'] = $t_id;
            $data['u_id'] = session(C("SESSION_USER_ID"));
            $data['thesis'] = $path;
            $data['createtime'] = date("Y-m-d H:i:s");

            if (M("Thesis")->save($data)) {
                $this->success('修改成功！');
            } else {
                $this->success('修改失败！');
            }
        }
    }
?>